//
//  z_Network.m
//  Template
//
//  Created by zhoubl on 13-6-5.
//  Copyright (c) 2013年 cdeledu. All rights reserved.
//

#import "z_Network.h"

@implementation z_Network

/**
 *	@brief	HTTP协议GET方式请求数据
 *
 *	@param 	value 	字典型传输数据
 *	@param 	url 	访问地址
 *	@param 	delegate 	代理
 *
 *	@return	返回ASI类型
 */
+ (ASIHTTPRequest *)httpGet:(NSDictionary *)value Url:(NSString *)url Delegate:(id)delegate Tag:(NSInteger)tag
{
    NSEnumerator * enumeratorKey = [value keyEnumerator];
    NSString *urlString =[NSString stringWithFormat:@"%@?",url];
    NSArray *array=[enumeratorKey allObjects];
    for (int i=0;i<[array count];i++)
    {
        NSObject *object=[array objectAtIndex:i];
        NSString *key=[object description];
        if(i==[array count]-1)
        {
            urlString =[NSString stringWithFormat:@"%@%@=%@",urlString,key,[value valueForKey:key]];
        }
        else {
            urlString =[NSString stringWithFormat:@"%@%@=%@&",urlString,key,[value valueForKey:key]];
        }
    }
    urlString=[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    CLog(@"requestURL==%@",urlString);
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlString]];
    [request setDelegate:delegate];
    [request setRequestMethod:@"GET"];
    [request addRequestHeader:@"Content-Type" value:@"*/*"];
    [request setValidatesSecureCertificate:NO];
    [request setTimeOutSeconds:20];
    [request setTag:tag];
    return  request;
}

/**
 *	@brief	HTTP协议POST方式请求数据
 *
 *	@param 	value 	字典型传输数据
 *	@param 	url 	访问地址
 *	@param 	delegate 	代理
 *
 *	@return	返回ASI类型
 */
+ (ASIFormDataRequest *)httpPostValue:(NSDictionary *)value Url:(NSString *)url Delegate:(id)delegate Tag:(NSInteger)tag
{
    NSString *urlString=[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    ASIFormDataRequest*request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:urlString]];
    [request setDelegate:delegate];
    [request setRequestMethod:@"POST"];
    [request setTimeOutSeconds:20];
    [request setTag:tag];
    NSEnumerator * enumeratorKey = [value keyEnumerator];
    for (NSObject *object in enumeratorKey) {
        NSString* key=[object description];
//        [(ASIFormDataRequest *)request setPostValue:[value valueForKey:key]forKey:key];
        if ([key isEqualToString:@"headimg"] && [[value valueForKey:key] length]>0) {
            //添加图片信息
            [(ASIFormDataRequest *)request setFile:[value valueForKey:key] forKey:key];
        }else{
            [(ASIFormDataRequest *)request setPostValue:[value valueForKey:key] forKey:key];
        }
    }
    return request;
}

/**
 *	@brief	HTTP协议POST方式以Data方式向服务器上传数据
 *
 *	@param 	data 	数据
 *	@param 	url 	访问地址
 *	@param 	delegate 	代理
 *
 *	@return	返回ASI类型
 */
+ (ASIHTTPRequest *)httpDataValue:(NSData *)data Url:(NSString *)url Delegate:(id)delegate Tag:(NSInteger)tag
{
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
    [request setDelegate:delegate];
    [request appendPostData:data];
    [request setRequestMethod:@"POST"];
    [request setTag:tag];
    [request startAsynchronous];
    
    return request;
}

//Mark: 生成SOAP1.1版本的ASIHttp请求
// 参数 webServiceName：         远程WebService的名称
//参数 wsParameters：           调用参数数组，形式为[参数1名称，参数1值，参数2名称，参数2值⋯⋯]，如果没有调用参数，此参数为nil
+ (ASIHTTPRequest *)getASISOAPRequest:(NSString *)wsName wsParameters:(NSDictionary *)wsParas Delegate:(id)delegate Tag:(NSInteger)tag
{
//    ASIHTTPRequest *request = [self getASISOAP11Request:PWA_CURRENT_USER_MSG.serverurl webServiceFile:PWA_WEBSERVICEFILE xmlNameSpace:PWA_XMLNS webServiceName:wsName wsParameters:wsParas];
    ASIHTTPRequest *request = [self getASISOAP11Request:nil webServiceFile:nil xmlNameSpace:nil webServiceName:wsName wsParameters:wsParas];
    [request setDelegate:delegate];
    [request setTag:tag];
    return request;
}

/*
 //Mark: 生成SOAP1.1版本的ASIHttp请求
 参数 webURL：                 远程WebService的地址，不含*.asmx
 参数 webServiceFile：         远程WebService的访问文件名，如service.asmx
 参数 xmlNS：                  远程WebService的命名空间
 参数 webServiceName：         远程WebService的名称
 参数 wsParameters：           调用参数数组，形式为[参数1名称，参数1值，参数2名称，参数2值⋯⋯]，如果没有调用参数，此参数为nil
 */
+ (ASIHTTPRequest *)getASISOAP11Request:(NSString *) WebURL
                         webServiceFile:(NSString *) wsFile
                           xmlNameSpace:(NSString *) xmlNS
                         webServiceName:(NSString *) wsName
                           wsParameters:(NSDictionary *) wsParas
{
    //1、初始化SOAP消息体
    NSString * soapMsgBody1 = [[NSString alloc] initWithFormat:
                               @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                               "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n"
                               "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" \n"
                               "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
                               "<soap:Body>\n"
                               "<%@ xmlns=\"%@\">\n", wsName, xmlNS];
    NSString * soapMsgBody2 = [[NSString alloc] initWithFormat:
                               @"</%@>\n"
                               "</soap:Body>\n"
                               "</soap:Envelope>", wsName];
    
    //2、生成SOAP调用参数
    NSString * soapParas = [[NSString alloc] init];
    soapParas = @"";
    if (wsParas) {
        int i = 0;
        NSArray *array = [wsParas allKeys];
        for (i = 0; i < [array count]; i++) {
            NSString *key = [array objectAtIndex:i];
            soapParas = [soapParas stringByAppendingFormat:@"<%@>%@</%@>\n",key,[wsParas objectForKey:key],key];
        }
    }
    
    //3、生成SOAP消息
    NSString * soapMsg = [soapMsgBody1 stringByAppendingFormat:@"%@%@", soapParas, soapMsgBody2];
    
    //请求发送到的路径
    NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", WebURL, wsFile]];
    
    CLog(@"ASIURL=====%@===",url);
    
    //NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    ASIHTTPRequest * theRequest = [ASIHTTPRequest requestWithURL:url];
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];
    
    //以下对请求信息添加属性前四句是必有的，第五句是soap信息。
    [theRequest addRequestHeader:@"Content-Type" value:@"text/xml; charset=utf-8"];
    [theRequest addRequestHeader:@"SOAPAction" value:[NSString stringWithFormat:@"%@%@", xmlNS, wsName]];
    
    [theRequest addRequestHeader:@"Content-Length" value:msgLength];
    [theRequest setRequestMethod:@"POST"];
    [theRequest appendPostData:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
    
    //设置超时时间
    [theRequest setTimeOutSeconds:20];
        
    [theRequest setDefaultResponseEncoding:NSUTF8StringEncoding];
    
    return theRequest;
}

@end
